/*
 * @Author: your name
 * @Date: 2021-04-15 20:12:17
 * @LastEditTime: 2021-04-24 20:56:04
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: /first_flutter_app/lib/indexPage/table.dart
 */

// import 'package:first_flutter_app/util/IconText.dart';
import 'package:first_flutter_app/controller/orderPageController.dart';
import 'package:first_flutter_app/controller/tablePageController.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:giffy_dialog/giffy_dialog.dart';

class tableShow extends StatefulWidget {
  Map tableInfo = {};
  int tableIndex = 0;
  tableShow({Key key, this.tableInfo, this.tableIndex}) : super(key: key);

  @override
  _tableShowState createState() => _tableShowState();
}

class _tableShowState extends State<tableShow> {
  int showColorFlage = 1;
  MaterialColor cc = Colors.grey;
  var tableMainShow = Container(
    child: Center(
      child: Icon(Icons.add),
    ),
  );
  var colorMap = {
    1: Colors.grey, //空闲态
    2: Colors.orange, //已开台
    // 3: Colors.green, // 已下单
    3: Colors.purple, // 已结账
  };

  Map<int, Widget> tableMainMap = {
    1: Container(
      child: Center(
        child: Icon(Icons.add),
      ),
    ), //空闲态
    2: Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [Text("￥100"), Text("4h53m")],
        ),
      ),
    ), //已开台
    // 3: Container(
    //   child: Center(
    //     child: Icon(Icons.post_add_sharp),
    //   ),
    // ), // 已下单
    3: Container(
      child: Center(
        child: Icon(Icons.cleaning_services),
      ),
    ), // 已结账
  };

  @override
  Widget build(BuildContext context) {
    return LongPressDraggable(
          ignoringFeedbackSemantics: true,
          data: widget.tableIndex,
          child: DragTarget<int>(
            builder: (context, data, rejects) {
              return Material(
                child: Column(
                  children: [
                    Container(
                        margin: EdgeInsets.only(top: 5),
                        width: ScreenUtil().setWidth(145),
                        height: ScreenUtil().setHeight(80),
                        decoration: BoxDecoration(
                          border: new Border.all(color: Colors.black12, width: 0.5),
                          color: cc,
                          borderRadius: BorderRadius.only(
                              topLeft: Radius.circular(10.0),
                              topRight: Radius.circular(10.0)), //
                        ),
                        child: InkWell(
                            onTap: () {
                              HomeController.hc.setShowOrderDetails();
                            },
                            child: Flex(
                              direction: Axis.horizontal,
                              children: [
                                // 桌台名称
                                Expanded(
                                    flex: 6,
                                    child: Container(
                                        margin: EdgeInsets.only(left: 5.0),
                                        child: Text(
                                          widget.tableInfo["tableName"],
                                          style: TextStyle(
                                            color: Colors.white,
                                            fontSize: 13.0,
                                          ),
                                          overflow: TextOverflow.ellipsis,
                                        ))),
                                // 占台人数
                                Expanded(
                                    flex: 2,
                                    child: Text(
                                        widget.tableInfo["person"].toString() +
                                            "/" +
                                            widget.tableInfo["tableMaxOcup"].toString(),
                                        style: TextStyle(color: Colors.white, fontSize: 10.0))),
                              ],
                            )
                        )
                    ),
                    Container(
                      width: ScreenUtil().setWidth(145),
                      height: ScreenUtil().setHeight(200),
                      decoration: BoxDecoration(
                        border: new Border.all(color: Colors.black12, width: 0.5),
                        color: Colors.white,
                        borderRadius: BorderRadius.only(
                            bottomLeft: Radius.circular(10.0),
                            bottomRight: Radius.circular(10.0)), //
                      ),
                      child: GetBuilder<HomeController>(
                        builder: (_) => InkWell(
                          child: tableMainShow,
                          onTap: () {
                            setState(() {
                              if(this.showColorFlage < 2) {
                                cc = this.colorMap[++this.showColorFlage];
                              }
                              tableMainShow = tableMainMap[this.showColorFlage];
                              // if (this.showColorFlage == 2) Get.toNamed("/orderPage");
                              OrderController.oc.currentSelectTable = widget.tableInfo;
                              Get.toNamed("/orderPage");
                            });

                          },
                        ),
                      ),
                    ),
                  ],
                ),
              );
            },
            // 手指拖着一个widget从另一个widget头上滑走时会调用
            onLeave: (data) {
              print(widget.tableIndex);
              print('---$data is Leaving item ---');
            },
            // 松手时，是否需要将数据给这个widget，因为需要在拖动时改变UI，所以在这里直接修改数据源
            onWillAccept: (data) {
              print(widget.tableIndex);
              print('---(target) will accept item (drag)$data---');
              return true;
            },
            // 松手时，如果onWillAccept返回true，那么就会调用
            onAccept: (data) {
              print(widget.tableIndex);

              print('---(onAccept) accept item (drag)$data---');
              setState(() {
                cc = colorMap[0];
                tableMainShow = tableMainMap[0];
              });
              // TODO 松手时交换数据并刷新 UI
            },
          ),
          feedback: Material(
            child: Column(
              children: [
                Container(
                    margin: EdgeInsets.only(top: 5),
                    width: ScreenUtil().setWidth(145),
                    height: ScreenUtil().setHeight(80),
                    decoration: BoxDecoration(
                      border: new Border.all(color: Colors.black12, width: 0.5),
                      color: cc,
                      borderRadius: BorderRadius.only(
                          topLeft: Radius.circular(10.0),
                          topRight: Radius.circular(10.0)), //
                    ),
                    child: InkWell(
                        onTap: () {
                          HomeController.hc.setShowOrderDetails();
                        },
                        child: Flex(
                          direction: Axis.horizontal,
                          children: [
                            // 桌台名称
                            Expanded(
                                flex: 6,
                                child: Container(
                                    margin: EdgeInsets.only(left: 5.0),
                                    child: Text(
                                      widget.tableInfo["tableName"],
                                      style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 13.0,
                                      ),
                                      overflow: TextOverflow.ellipsis,
                                    ))),
                            // 占台人数
                            Expanded(
                                flex: 2,
                                child: Text(
                                    widget.tableInfo["person"].toString() +
                                        "/" +
                                        widget.tableInfo["tableMaxOcup"].toString(),
                                    style: TextStyle(color: Colors.white, fontSize: 10.0))),
                          ],
                        )
                    )
                ),
                Container(
                  width: ScreenUtil().setWidth(145),
                  height: ScreenUtil().setHeight(200),
                  decoration: BoxDecoration(
                    border: new Border.all(color: Colors.black12, width: 0.5),
                    color: Colors.white,
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(10.0),
                        bottomRight: Radius.circular(10.0)), //
                  ),
                  child: GetBuilder<HomeController>(
                    // init: HomeController(),
                    builder: (_) => InkWell(
                      child: tableMainShow,
                      // onDoubleTap: () {
                      //   if (this.showColorFlage >= 2) HomeController.hc.setShowOrderDetails();
                      // },
                      onLongPress: () {
                        /*
                        showDialog(
                            context: context,
                            builder: (_) => AssetGiffyDialog(
                              // key: keys[5],
                              // Image.asset("images/1.png", fit: BoxFit.cover)
                              buttonOkText: Text("确定",
                                  style: TextStyle(color: Colors.white)),
                              buttonCancelText: Text("再想想",
                                  style: TextStyle(color: Colors.white)),
                              image: Image.asset(
                                'images/iconfont.gif',
                                fit: BoxFit.cover,
                              ),
                              title: Text(
                                '操作',
                                textAlign: TextAlign.center,
                                style: TextStyle(
                                    fontSize: ScreenUtil().setSp(100),
                                    fontWeight: FontWeight.w600),
                              ),
                              // entryAnimation: EntryAnimation.DEFAULT,
                              description: Text(
                                '确定清台吗?',
                                textAlign: TextAlign.center,
                                style:
                                TextStyle(fontSize: ScreenUtil().setSp(150)),
                              ),
                              onOkButtonPressed: () {
                                setState(() {
                                  this.showColorFlage = 1;
                                  cc = this.colorMap[this.showColorFlage];
                                  tableMainShow =
                                  tableMainMap[this.showColorFlage];
                                });
                                Navigator.of(context).pop();
                                HomeController.hc.setNoShowOrderDetails();
                              },
                            )
                        );
                        */
                      },
                      onTap: () {
                        setState(() {
                          if(this.showColorFlage < 2) {
                            cc = this.colorMap[++this.showColorFlage];
                          }
                          tableMainShow = tableMainMap[this.showColorFlage];
                          // if (this.showColorFlage == 2) Get.toNamed("/orderPage");
                          Get.toNamed("/orderPage");
                        });

                      },
                    ),
                  ),
                ),
              ],
            ),
          ),
          childWhenDragging: Material(
            child: Column(
              children: [
                Container(
                  margin: EdgeInsets.only(top: 5),
                  width: ScreenUtil().setWidth(145),
                  height: ScreenUtil().setHeight(80),
                  decoration: BoxDecoration(
                    color: Colors.grey,
                    borderRadius: BorderRadius.only(
                        topLeft: Radius.circular(10.0),
                        topRight: Radius.circular(10.0)), //
                  ),
                ),
                Container(
                  width: ScreenUtil().setWidth(145),
                  height: ScreenUtil().setHeight(200),
                  decoration: BoxDecoration(
                    color: Colors.grey,
                    borderRadius: BorderRadius.only(
                        bottomLeft: Radius.circular(10.0),
                        bottomRight: Radius.circular(10.0)), //
                  ),
                ),
              ],
            ),
          ),
          onDragStarted: () {
            print("onDragStarted");
          },
          onDragEnd: (DraggableDetails details) {
            print("onDragEnd : $details");
          },
          onDraggableCanceled: (Velocity velocity, Offset offset) {
            print('onDraggableCanceled velocity:$velocity,offset:$offset');
          },
          onDragCompleted: () {
            print('onDragCompleted');
        },
    );
  }
}
